$(function() {
    layui.use(['form', 'upload', 'jquery'],
    function() {
        var form = layui.form,
        upload = layui.upload,
        jq = layui.jquery;
        //上传图片
        upload.render({
            elem: '.upimg',
            url: "/index.php/admin/up/up.html",
            accept: 'file',
            //size: 100,
            exts: 'jpg|png|gif|bmp|jpeg',
            done: function(res) {
                if (res.code == '1') {
                    var up = jq('.upimg').attr('up');
                    jq('input[id=' + up + ']').val(res.path);
                    return layer.msg(res.msg);
                } else {
                    return layer.msg(res.msg);
                }
            }
        });
        //附件
        upload.render({
            elem: '.up',
            url: "/index.php/admin/up/up.html",
            accept: 'file',
            done: function(res) {
                if (res.code == '1') {
                    var up = jq('.up').attr('up');
                    jq('input[id=' + up + ']').val(res.path);
                    return layer.msg(res.msg);
                } else {
                    return layer.msg(res.msg);
                }
            }
        });
    
    })
    //通用表单提交跳转
    layui.use(['jquery', 'form'],
    function() {
        var form = layui.form,
        jq = layui.jquery;
        form.on('submit(cms)',
        function(data) {
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            var urls = jq('.form').attr('url');
            var t_url = jq('.form').attr('t_url');
            var param = data.field;
            jq.post(urls, param,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        location.href = t_url;
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
            return false;
        });
        //通用表单提交跳转1
        form.on('submit(cms1)',
        function(data) {
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            var urls = jq('.form1').attr('url');
            var t_url = jq('.form1').attr('t_url');
            var param = data.field;
            jq.post(urls, param,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        location.href = t_url;
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
            return false;
        });
        //无弹出触发链接弹出收藏
        jq('.del').click(function() {
            var url = jq(this).attr('url');
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            jq.post(url,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        location.reload();
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
        });
        // 加载动画
        $(".a").click(function(){
            var index = layer.load(1, {
            shade: [0.5,'#000000'],
            time: 1000
        });
        });
        //弹出触发链接弹出
        jq('.del_t').click(function() {
            var name = jq(this).attr('name');
            var url = jq(this).attr('url');
            layer.confirm(name,
            function(index) {
                loading = layer.load(2, {
                    shade: [0.2, '#000']
                });
                jq.post(url,
                function(data) {
                    if (data.code == 1) {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 1000
                        },
                        function() {
                            location.reload();
                        });
                    } else {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 2,
                            anim: 6,
                            time: 1000
                        });
                    }
                });
            });
        });
    })
    
    //用户充值
    $(".cz_user").click(function() {
        layer.prompt({
            title: '充值-多少元',
            formType: 3
        },
        function(text, index) {
            $.ajax({
                url: "/index.php/index/alipay/paysubmit?jiage=" + text,
                type: "GET",
                dataType: "json",
                success: function(data) {
                    loading = layer.load(3);
                    if (data.code == 1) {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 1000
                        },
                        function() {
                            $('body').append(data.url);
                        });
                    } else {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 2,
                            anim: 6,
                            time: 1000
                        });
                    }
                }
            });
        });
    });
    //用户提现
    $(".tixian_user").click(function() {
        layer.prompt({
            title: '提现',
            formType: 3
        },
        function(text, index) {
            $.ajax({
                url: "/index.php/index/api/tixian_user?jiage=" + text,
                type: "GET",
                dataType: "json",
                success: function(data) {
                    loading = layer.load(3);
                    if (data.code == 1) {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 1000
                        },
                        function() {
                            location.reload();
                        });
                    } else {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 2,
                            anim: 6,
                            time: 1000
                        });
                    }
                }
            });
        });
    });
    // 文章回复
    $(".huifu").click(function() {
        var data = $(this).attr('data');
        var name = $(this).attr('name');
        layer.prompt({
            title: '回复' + name,
            formType: 2
        },
        function(content, index) {
            $.ajax({
                url: data + "?content=" + content,
                type: "GET",
                dataType: "json",
                success: function(data) {
                    loading = layer.load(3);
                    if (data.code == 1) {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 1000
                        },
                        function() {
                            location.reload();
                        });
                    } else {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 2,
                            anim: 6,
                            time: 1000
                        });
                    }
                }
            });
        });
    });
    //发送验证邮箱或者手机
    layui.use('form',
    function() {
        var form = layui.form,
        jq = layui.jquery;
        jq('#phoneormail').click(function() {
            var obj = $(this);
            var options = {
                seconds: 60,
                type: 'REG'
            };
            var name = $("#name").val();　
            if (name == "") {　　　
                layer.msg('邮箱或者手机号码必填');　　　　
                return false;　　
            } 
            loading = layer.load(0, {
                shade: [0.2, '#000']
            });
            jq.getJSON('/index.php/index/api/yanzhengma.html?name=' + name,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        countDown(60, obj);
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
        });
    });
    function countDown(seconds, obj) {
        if (seconds > 1) {
            seconds--;
            $(obj).attr('disabled', true).html(seconds + "秒"); //禁用按钮
            setTimeout(function() {
                countDown(seconds, obj);
            },
            1000);
        } else {
            $(obj).attr('disabled', false).html("获取验证码"); //启用按钮
        }
    }
    layui.use(['form', 'upload'],
    function() {
        var form = layui.form,
        upload = layui.upload,
        jq = layui.jquery;
        //收藏
        jq('.shoucang').click(function() {
            var urls = jq('.shoucang').attr('url');
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            jq.getJSON(urls,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        $(".shoucangy").html("已收藏");
                    });
                } else if (data.code == 0) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        $(".shoucangy").html("收藏");
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
        });
        //点赞
        jq('.dianzan').click(function() {
            var urls = jq('.dianzan').attr('url');
            var zan = jq('.dianzan').attr('zan');
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            jq.getJSON(urls,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        $(".dianzans").html(zan);
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
        });
    })
    // 编辑器
    function bianjiqi() {
        var editor = new wangEditor('textarea');
        	editor.config.uploadImgUrl = 'index.php/admin/up/pic.html';
        	editor.config.uploadImgFileName = 'FileName';
            editor.config.jsFilter = false;
            editor.config.pasteFilter = false;
            editor.config.menus =  [
                'bold',
                'eraser',
                'forecolor',
                'quote',
                'fontsize',
                '|',
                'alignleft',
                'aligncenter',
                'alignright',
                'link',
                'unlink',
                'img',
                'insertcode',
            ];
        editor.create();
    }
    layui.use('layer', function(){
        var layer = layui.layer;
        // 通用表单提交
        $('#link_form').on('click', function(){
            var form = new FormData($("#itemForm")[0]);
            var url = $(this).attr('url');
            $.ajax({
                url: url,
                type: "post",
                data:form,
                dataType:'json', 
                processData:false,
                contentType:false,
                success: function(data) {
                    loading = layer.load(3);
                    if (data.code == 1) {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 1000
                        },function() {
                            location.reload();
                        });
                    } else {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 2,
                            anim: 6,
                            time: 1000
                        });
                    }
                }
            });
        });
    
        // 弹出相片
        layer.photos({
            photos: '.xiangce'
            ,anim: 0
        }); 
        // 弹出相片
        layer.photos({
            photos: '.wangEditor-container'
            ,anim: 0
        }); 
         
        //弹出发送信息
        $(".k_f").click(function() {
            var data_url = $(this).attr('data_url');
            var title = $(this).attr('title');
            layer.prompt({
                title: title,
                formType: 2
            },
            function(text, index) {
                $.ajax({
                    url: data_url+"?config=" + text,
                    type: "GET",
                    dataType: "json",
                    success: function(data) {
                        loading = layer.load(3);
                        if (data.code == 1) {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 1000
                            },
                            function() {
                                location.reload();
                            });
                        } else {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 2,
                                anim: 6,
                                time: 1000
                            });
                        }
                    }
                });
            });
        });
        $('.yemian').on('click', function(){
            var css = $(this).attr("css");
            var ws = $(this).attr("w");
            var hs = $(this).attr("h");
            var tblr = $(this).attr("tblr");
            var anim = $(this).attr("anim");
            var skin = $(this).attr("skin");
            var title = $(this).attr("title");
            if(!title){
                var title = false;
            }
            layer.open({
              type: 1,
              offset: tblr,
              skin: skin, 
              shade: 0.5,
              closeBtn: 0,
              title: title,
              shadeClose: true,  
              anim: anim,
              area: [''+ws+'', ''+hs+''], //宽高
              content: $('.'+css),
            });
         });
        $('.yemianf').on('click', function(){
            var url = $(this).attr("url");
            var ws = $(this).attr("w");
            var hs = $(this).attr("h");
            var tblr = $(this).attr("tblr");
            var anim = $(this).attr("anim");
            var skin = $(this).attr("skin");
            var title = $(this).attr("title");
            if(!title){
                var title = false;
            }
            layer.open({
              type: 2,
              offset: tblr,
              skin: skin, 
              shade: 0.5,
              title: title,
              closeBtn: 1,
              shadeClose: true, 
              anim: anim,
              area: [''+ws+'', ''+hs+''], //宽高
              content: url,
            });
         });
        // 触发链接
        $('.chufa').on('click', function(){
            var url = $(this).attr('url');
            $.ajax({
                url: url,
                type: "get",
                dataType: "json",
                success: function(data) {
                    if (data.code == 1) {
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 1000
                        },function() {
                            location.reload();
                        });
                    } else {
                        layer.msg(data.msg, {
                            icon: 2,
                            anim: 6,
                            time: 1000
                        });
                    }
                }
            });
        });
        // 举报
        $('.jubao').on('click', function(){
            var data_url = $(this).attr('data_url');
            var title = $(this).attr('title');
            layer.prompt({
            title: '举报',
            value: title, 
            formType: 2
            },function(content, index) {
                $.ajax({
                    url: data_url + "?content=" + content,
                    type: "GET",
                    dataType: "json",
                    success: function(data) {
                        loading = layer.load(3);
                        if (data.code == 1) {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 1000
                            },function() {
                                layer.close(index)
                            });
                        } else {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 2,
                                anim: 6,
                                time: 1000
                            });
                        }
                    }
                });
            });
        });
    });
	$('.top-icon').click(function(){
		$("html,body").animate({scrollTop:0},500);
	})
	$('.label-box span').click(function(){
		$(this).addClass('onlabel').siblings().removeClass('onlabel');
		$(".url").attr("action",$(this).attr("url"));
	});
});